package de.jmda.gui.swing.mvc;

import java.util.Set;

import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;

import org.apache.log4j.Logger;

import de.jmda.mproc.Processor;

/**
 * Filters type declarations annotated with {@link ControllerSupportEnabled},
 * instantiates and calls {@link ControllerSupportEnabledProcessor}s.
 *
 * @see #process(Set, RoundEnvironment)
 *
 * @author roger.jmda@gmail.com
 */
@SupportedAnnotationTypes(value = { "de.jmda.gui.swing.mvc.ControllerSupportEnabled" })
@SupportedSourceVersion(value = SourceVersion.RELEASE_6)
public class ControllerSupportEnabledProcessor extends Processor
{
	private final static Logger LOGGER =
			Logger.getLogger(ControllerSupportEnabledProcessor.class);

	@Override
  public boolean process()
  {
		LOGGER.debug("starting");
		
		// this already fetches the desired type elements as long as @Target of
		// ControllerSupportEnabled is ElementType.TYPE 
		Set<? extends Element> elements =
				roundEnvironment.getElementsAnnotatedWith(
						ControllerSupportEnabled.class);
		LOGGER.debug("found " + elements.size() + " elements");
		// make sure we only have type elements even if @Target of
		// ControllerSupportEnabled is NOT ElementType.TYPE
		Set<? extends TypeElement> typeElements = ElementFilter.typesIn(elements);
		LOGGER.debug("found " + typeElements.size() + " type elements");

		for (TypeElement typeElement : typeElements)
		{
			LOGGER.info("processing [" + typeElement.getQualifiedName() + "]");

			ControllerSupportEnabledGenerator generator =
					new ControllerSupportEnabledGenerator(typeElement);

			generator.generate();
		}

		LOGGER.debug("finished");

		return false;
  }

	@Override
  protected Class<?>[] getSupposedSupportedAnnotationTypes()
  {
	  return new Class<?>[] { ControllerSupportEnabled.class };
  }
}